// pkg_pz/pages/TwoYuyue/TwoYuyue.js
const app = getApp();
import request from '../../../utils/http.js'
let reg = {};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderFormId:0,
    tabIndex:0,
    secondPayMoney:0,
    qiWangFuWuShiJian:'',
    firstPayMoney:0,//预付款金额
    lianXiRenPhone:'',
    lianXiRenXingMing:'',
    productName:'',
    chuangJianRiQi:'',
    // JzrName:'',//就诊人姓名填写
    // JzrSfz:'',//就诊人身份证号填写
    // JzrPhone:'',//就诊人电话填写

    jiuZhenRenXingMing:'',//就诊人姓名
    jiuZhenRenPhone:'',//就诊人电话
    jiuZhenRenShenFenZheng:'',//就诊人身份证
    dingDanZhuangTai:0,

    infoIsAll: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.setData({
      orderFormId:options.orderFormId,
      dingDanZhuangTai:options.dingDanZhuangTai,
    })
    that.getSecondPayMoney();
    that.getOrderFormInfo();
  },


  //获取一个订单相关信息
  getOrderFormInfo: function () {
      var that = this
      let data = {
        orderFormId:that.data.orderFormId,
      }
      request.get('peiZhenOrderForm/getOrderFormInfo', data, res => {
        console.log("获取一个订单相关信息:" + JSON.stringify(res.data))
        that.setData({
          // JiuZenList:res.data,
          jiuZhenRenXingMing:res.data.jiuZhenRenXingMing,
          jiuZhenRenPhone:res.data.jiuZhenRenPhone,
          jiuZhenRenShenFenZheng:res.data.jiuZhenRenShenFenZheng,
          productName:res.data.productName,
          lianXiRenXingMing:res.data.lianXiRenXingMing,
          lianXiRenPhone:res.data.lianXiRenPhone,
          firstPayMoney:res.data.firstPayMoney,
          qiWangFuWuShiJian:res.data.qiWangFuWuShiJian,
          firstPayMoney:res.data.firstPayMoney,
          secondPayMoney:res.data.secondPayMoney,
          chuangJianRiQi:res.data.chuangJianRiQi,
        })
        if (that.data.jiuZhenRenPhone!=''&&that.data.jiuZhenRenShenFenZheng!=''&&that.data.jiuZhenRenXingMing!='') {
          this.setData({
            infoIsAll: true
          })
        }
  
      }, err => {
        wx.showToast({
          title: '获取信息失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      })
    
    },

  //获取第二次应该支付的金额，还有一些订单相关信息
  getSecondPayMoney: function () {
    var that = this
    let data = {
      orderFormId:that.data.orderFormId,
    }
    request.get('peiZhenOrderForm/getSecondPayMoney', data, res => {
      console.log("获取第二次应该支付的金额:" + JSON.stringify(res.data))
      that.setData({
        secondPayMoney:res.data.secondPayMoney,
        qiWangFuWuShiJian:res.data.qiWangFuWuShiJian,
        firstPayMoney:res.data.firstPayMoney,
        lianXiRenPhone:res.data.lianXiRenPhone,
        lianXiRenXingMing:res.data.lianXiRenXingMing,
        productName:res.data.productName,
      })
  
    }, err => {
      wx.showToast({
        title: '获取信息失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })
  
  },
    
  //提交信息
  secondSbumitOrderForm: function () {
    var that = this
    // 验证姓名、电话和身份证是否已填写以及格式是否正确
  let tipContent = '';

  if (!that.data.jiuZhenRenXingMing) {
    tipContent += '请填写就诊人姓名';
  }
  
  if (!that.data.jiuZhenRenPhone || that.data.jiuZhenRenPhone.length !== 11) {
    if (tipContent) tipContent += '，';
    if (!that.data.jiuZhenRenPhone) {
      tipContent += '请填写就诊人电话号码';
    } else {
      tipContent += '联系电话应为11位数字';
    }
  }
  if (!that.data.jiuZhenRenShenFenZheng || that.data.jiuZhenRenShenFenZheng.length !== 18) {
    if (tipContent) tipContent += '，';
    if (!that.data.jiuZhenRenShenFenZheng) {
      tipContent += '请填写就诊人身份证号码';
    } else {
      tipContent += '身份证号码应为18位数字或字母（X）';
    }
  }
  if (tipContent) {
    wx.showToast({
      title: tipContent, 
      icon: "none", 
      duration: 1000, 
      mask: false, 
    });
    return; 
  }
  wx.showModal({
    title: '提交确认',
    content: '请确认您的就诊人信息!!!',
    showCancel: true,
    cancelText: '取消',
    confirmText: '确定',
    success(confirmRes) {
      if (confirmRes.confirm) {
    let data = {
      openId: wx.getStorageSync(app.globalData.OPENID),
      orderFormId:that.data.orderFormId,
      jiuZhenRenXingMing:that.data.jiuZhenRenXingMing,
      jiuZhenRenPhone:that.data.jiuZhenRenPhone,
      jiuZhenRenShenFenZheng:that.data.jiuZhenRenShenFenZheng,
    }
    request.get('peiZhenOrderForm/secondSbumitOrderForm', data, res => {
      console.log("sssss:" + JSON.stringify(res.data))
      if (res.data.status == 1) {
        wx.requestPayment({
          timeStamp: res.data.map.timeStamp,
          nonceStr: res.data.map.nonce_str,
          package: 'prepay_id=' + res.data.map.prepay_id,
          signType: 'MD5',
          paySign: res.data.map.paySign,
          success: function (ress) {
            that.syncData()
            wx.showToast({
              title: "支付成功!!!", // 提示的内容
              icon: "none", // 图标，默认success
              duration: 1500, // 提示的延迟时间，默认1500
              mask: false, // 是否显示透明蒙层，防止触摸穿透
            })
            setTimeout(() => {
              wx.redirectTo({
                url: '/pkg_pz/pages/Pzyuyue/Pzyuyue?index=2',
              });
            }, 1000); // 延迟1秒后进行跳转
          },
          fail: function (ress) {
            wx.showToast({
              title: "支付失败！", // 提示的内容
              icon: "none", // 图标，默认success
              duration: 1000, // 提示的延迟时间，默认1500
              mask: false, // 是否显示透明蒙层，防止触摸穿透
            })
            setTimeout(() => {
              wx.redirectTo({
                url: '/pkg_pz/pages/Pzyuyue/Pzyuyue?index=1',
              });
            }, 1000); // 延迟1秒后进行跳转
          },
          complete: function (ress) {
          }
        });

      } else { //订单支付失败
        wx.showToast({
          title: "订单提交失败！", // 提示的内容
          icon: "none", // 图标，默认success
          duration: 2000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      }
 
    }, err => {
      wx.showToast({
        title: '提交失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })
  } else if (confirmRes.cancel) {
    console.log('用户点击了取消');
   }
  }, 
   });
  },


   //虚拟支付
   zhifu: function (dingDanHao) {
    var that = this
    let data = {
      dingDanHao:dingDanHao
    }
    request.get('peiZhenOrderForm/xiuNiZhiFu', data, res => {
      wx.hideLoading();
      wx.showToast({
        title: '支付成功', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pkg_pz/pages/Pzyuyue/Pzyuyue?index=2',
        });
      }, 1500); // 延迟2秒后进行跳转
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

    //姓名
  onNameInput: function(event) {
      this.setData({
        jiuZhenRenXingMing: event.detail.value,
      });
      this.isAllInfo()
    },
   //身份证号
  onidcardInput: function(event) {
    this.setData({
      jiuZhenRenShenFenZheng: event.detail.value,
    });
    this.isAllInfo()
  },
  //电话
  onPhoneInput: function(event) {
    this.setData({
      jiuZhenRenPhone: event.detail.value,
    });
    this.isAllInfo()
  },

  isAllInfo() {
    if (this.data.jiuZhenRenPhone.length == 11 && this.data.jiuZhenRenXingMing != '' && this.data.jiuZhenRenShenFenZheng.length ==18) {
      this.setData({
        infoIsAll: true
      })
    } else {
      this.setData({
        infoIsAll: false
      })
    }
  },
  ts: function () {
    wx.showToast({
      title: '请填写就诊人信息!!!', // 提示的内容
      icon: "none", // 图标，默认success
      duration: 2000, // 提示的延迟时间，默认1500
      mask: false, // 是否显示透明蒙层，防止触摸穿透
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})